package com.cn.struts2;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.cn.http.HttpClients;

/**
 * 已弃用，详见 {@link com.cn.web.controller.HttpBusinessController}
 */
@Deprecated
public class HttpAction extends BaseAction {

	/**
	 * 序列化
	 */
	private static final long serialVersionUID = -7184241315048865684L;

	/**
	 * 打印日志
	 */
	private Logger logger = Logger.getLogger(HttpAction.class);

	// ciphertext="{'businessRecord':'123456','smsOperator':'中国移动','sendContent':'给我一个老北京冰棍','sendNumber': '18301255287#18261045789#15195224781'}";
	// ciphertext="{'businessRecord':'213456','smsOperator':'1000','sendContent':'给我一个随便','sendNumber':'13612345678'}";
	/**
	 * JSON字符串
	 */
	private String ciphertext;
	/**
	 * http客户端
	 */
	private HttpClients httpClients;

	public HttpClients getHttpClients() {
		return httpClients;
	}

	public void setHttpClients(HttpClients httpClients) {
		this.httpClients = httpClients;
	}

	public String getCiphertext() {
		return ciphertext;
	}

	public void setCiphertext(String ciphertext) {
		this.ciphertext = ciphertext;
	}

	/**
	 * 过滤手机号码并发送
	 * 
	 * @throws JSONException
	 */
	public void httpchannel() {

		
			this.httpchannelForResult();
		
		

	}
	
	/**
	 * 过滤手机号码并发送
	 * 
	 * @throws JSONException
	 */
	public void httpchannelForResult()  {
		JSONObject jo = new JSONObject();
		try {
			String result = httpClients.httpClientsChannel(ciphertext,
					getIpAddr());

			
			if ("success".equals(result)) {
				jo.put("resultCode", "1");
				jo.put("errorDesc", "发送成功");
			} else {
				jo.put("resultCode", "2");
				jo.put("errorDesc", result);
			}
//			logger.debug("jo=="+jo.toString());
			this.getResponse().setContentType("text/json;charset=GBK");
			this.getResponse().setCharacterEncoding("GBK");
			
			this.getResponse().getWriter().append(jo.toString());
//			this.getResponse().getWriter().write(jo.toString());
			logger.debug("reslut:" + jo.toString());
			
		} catch (Exception e1) {
			
			logger.debug("reslut:" + "{\"resultCode\":\"1\";\"errorDesc\":\"json异常\"}");
		}
		
		
		

	}

	/**
	 * 过滤手机号码并发送
	 * 
	 * @param json
	 *            JSON格式的字符串
	 */
	public void httpchannel(String json) {
		// try {

		String result = httpClients.httpClientsChannel(json, getIpAddr());
		// getResponse().setCharacterEncoding("GBK");
		// PrintWriter out = getResponse().getWriter();
		logger.debug("#############" + result);
		// out.print(result);
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// }
	}

	/**
	 * 获得IP
	 * 
	 * @return IP
	 */
	public String getIpAddr() {
		try {
			String ip = getRequest().getHeader("x-forwarded-for");
			if (ip == null || ip.length() == 0
					|| "unknown".equalsIgnoreCase(ip)) {
				ip = getRequest().getHeader("Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0
					|| "unknown".equalsIgnoreCase(ip)) {
				ip = getRequest().getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0
					|| "unknown".equalsIgnoreCase(ip)) {
				ip = getRequest().getRemoteAddr();
			}
			logger.debug("ip:" + ip);
			return ip;
		} catch (Exception e) {
			logger.debug("获取IP异常");
			e.printStackTrace();
			return null;
		}
	}

}
